﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;
using Analysis.Entities;
using Dapper;
using System.Data;
using System.Data.SqlClient;

namespace Analysis.DAL.Repositories
{
    public class SCURepository : BaseDataRepository
    {
        public static DataTable FindSCU()
        {
            string strSQL = @"select * from SCU (nolock) where IsDelete = 0";
            return DataAccess.GetDataTable(strSQL);
        }
        
        public static bool CheckClientId(int scuId, string clientId)
        {
            string strSQL = "select convert(bit, case when exists(select 1 from SCU where ClientId = @ClientId and SCUId <> @SCUId) then 0 else 1 end) as Result";
            DataTable dt = DataAccess.GetDataTable(strSQL, new string[] { "ClientId", "SCUId" }, new object[] { clientId, scuId });

            return Convert.ToBoolean(dt.Rows[0][0]);
        }

        public static void EditSCU(int id, List<KeyValuePair<string, object>> data)
        {
            string strSQL;
            List<string> parameters = new List<string>();
            List<object> values = new List<object>();

            if (id == -1)
            {
                strSQL = "insert into SCU ";
                strSQL += GenerateInsertDynamic(data, parameters, values);
            }
            else
            {
                strSQL = "update SCU set ";
                strSQL += GenerateUpdateDynamic(data, parameters, values);
                strSQL += " where SCUId = " + id.ToString();
            }
            DataAccess.ExecuteSQL(strSQL, parameters.ToArray(), values.ToArray()); 
        }

        public static void DeleteSCU(string id)
        {
            DataAccess.ExecuteProcedure("spSCU_Delete", new string[] { "@SCUId" }, new object[] { id });
        }
    }
}
